﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eStock.hControl;
using HCommon;

namespace eStock
{
    public partial class frmQuote : hForm
    {
        public frmQuote()
        {
            InitializeComponent();
        }

        private void frmUser_Load(object sender, EventArgs e)
        {
            datFromDate.Value = DateTime.Now.AddDays(-7);
            LoadGrid();
        }

        private void grdMain_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (grdMain.Rows.Count == 0 || e.RowIndex < 0)
                {
                    return;
                }
                frmQuote_Add QuoteAdd = new frmQuote_Add();
                QuoteAdd.UpdateKey = grdMain.Rows[e.RowIndex].Cells["QuoteNo"].Value.ToString();
                QuoteAdd.FormMode = Mode.Update;
                QuoteAdd.ShowDialog();
                if (QuoteAdd != null)
                {
                    LoadGrid();
                }
            }
            catch (Exception ex)
            {
                Common.GhiLog("grdMain_CellDoubleClick", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        private void btnQry_Click(object sender, EventArgs e)
        {
            LoadGrid();
        }

        private void LoadGrid()
        {
            try
            {
                DataTable dt = new DataTable();
                dt = Dao.Get_Table("Exec spQuote_Q N'" + Common.xoaNhay(datFromDate.Value.ToString("yyyyMMdd")) + "',N'" + Common.xoaNhay(datToDate.Value.ToString("yyyyMMdd")) + "',N'" + Common.xoaNhay(txtQuoteNo.Text) + "',N'" + Common.xoaNhay(txtCustomerCd.Text) + "',N'" + Common.xoaNhay(txtUserName.Text) + "','" + cmbStatus.SelectedValue.ToString() + "'");
                grdMain.DataSource = dt;
            }
            catch (Exception ex)
            {
                Common.GhiLog("LoadGrid", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            frmQuote_Add QuoteAdd = new frmQuote_Add();
            QuoteAdd.FormMode = Mode.Insert;
            QuoteAdd.ShowDialog();
            if (QuoteAdd != null)
            {
                LoadGrid();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void grdMain_hDelete(object sender, EventArgs e)
        {
            try
            {
                int delCnt = 0;
                foreach (DataGridViewRow row in grdMain.SelectedRows)
                {
                    if (tblQuote.Delete(row.Cells["QuoteNo"].Value.ToString()) > 0)
                    {
                        grdMain.Rows.Remove(row);
                        delCnt++;
                    }
                }
                Dialog.ShowMess(Dialog.mType.ok, "Đã xóa thành công " + delCnt + " bản ghi.");
            }
            catch (Exception ex)
            {
                Common.GhiLog("grdMain_hDelete", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }


    }
}
